<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Training data generation using Icosphere</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li><li class="navelem"><a class="el" href="../../d2/d42/tutorial_table_of_content_cnn_3dobj.html">CNN for 3D Object Classification and Pose Estimation</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Training data generation using Icosphere </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>Conduct a point cloud of camera view on sphere.</li>
<li>Generate training images using 3D model.</li>
</ul>
<h2>Code </h2>
<div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment"> * Software License Agreement (BSD License)</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *  Copyright (c) 2009, Willow Garage, Inc.</span></div><div class="line"><span class="comment"> *  All rights reserved.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *  Redistribution and use in source and binary forms, with or without</span></div><div class="line"><span class="comment"> *  modification, are permitted provided that the following conditions</span></div><div class="line"><span class="comment"> *  are met:</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *   * Redistributions of source code must retain the above copyright</span></div><div class="line"><span class="comment"> *     notice, this list of conditions and the following disclaimer.</span></div><div class="line"><span class="comment"> *   * Redistributions in binary form must reproduce the above</span></div><div class="line"><span class="comment"> *     copyright notice, this list of conditions and the following</span></div><div class="line"><span class="comment"> *     disclaimer in the documentation and/or other materials provided</span></div><div class="line"><span class="comment"> *     with the distribution.</span></div><div class="line"><span class="comment"> *   * Neither the name of Willow Garage, Inc. nor the names of its</span></div><div class="line"><span class="comment"> *     contributors may be used to endorse or promote products derived</span></div><div class="line"><span class="comment"> *     from this software without specific prior written permission.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span></div><div class="line"><span class="comment"> *  &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span></div><div class="line"><span class="comment"> *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS</span></div><div class="line"><span class="comment"> *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE</span></div><div class="line"><span class="comment"> *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,</span></div><div class="line"><span class="comment"> *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,</span></div><div class="line"><span class="comment"> *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;</span></div><div class="line"><span class="comment"> *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER</span></div><div class="line"><span class="comment"> *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT</span></div><div class="line"><span class="comment"> *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN</span></div><div class="line"><span class="comment"> *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE</span></div><div class="line"><span class="comment"> *  POSSIBILITY OF SUCH DAMAGE.</span></div><div class="line"><span class="comment"> *</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../de/d45/cnn__3dobj_8hpp.html">opencv2/cnn_3dobj.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d2/d4b/vizcore_8hpp.html">opencv2/viz/vizcore.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d9/d57/namespacecv_1_1cnn__3dobj.html">cv::cnn_3dobj</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> listDir(<span class="keyword">const</span> <span class="keywordtype">char</span> *path, std::vector&lt;String&gt;&amp; files, <span class="keywordtype">bool</span> r)</div><div class="line">{</div><div class="line">    DIR *pDir;</div><div class="line">    <span class="keyword">struct </span>dirent *ent;</div><div class="line">    <span class="keywordtype">char</span> childpath[512];</div><div class="line">    pDir = opendir(path);</div><div class="line">    memset(childpath, 0, <span class="keyword">sizeof</span>(childpath));</div><div class="line">    <span class="keywordflow">while</span> ((ent = readdir(pDir)) != NULL)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> (ent-&gt;d_type &amp; DT_DIR)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;.&quot;</span>) == 0 || strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;..&quot;</span>) == 0 || strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;.DS_Store&quot;</span>) == 0)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">continue</span>;</div><div class="line">            }</div><div class="line">            <span class="keywordflow">if</span> (r)</div><div class="line">            {</div><div class="line">                sprintf(childpath, <span class="stringliteral">&quot;%s/%s&quot;</span>, path, ent-&gt;d_name);</div><div class="line">                listDir(childpath,files,<span class="keyword">false</span>);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (strcmp(ent-&gt;d_name, <span class="stringliteral">&quot;.DS_Store&quot;</span>) != 0)</div><div class="line">                files.push_back(ent-&gt;d_name);</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga45dd56da289494ce874be2324856898f">sort</a>(files.begin(),files.end());</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> keys = <span class="stringliteral">&quot;{help | | demo :$ ./sphereview_test -ite_depth=2 -plymodel=../data/3Dmodel/ape.ply -imagedir=../data/images_all/ -labeldir=../data/label_all.txt -num_class=6 -label_class=0, then press &#39;q&#39; to run the demo for images generation when you see the gray background and a coordinate.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ite_depth | 3 | Iteration of sphere generation.}&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{plymodel | ../data/3Dmodel/ape.ply | Path of the &#39;.ply&#39; file for image rendering. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{imagedir | ../data/images_all/ | Path of the generated images for one particular .ply model. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{labeldir | ../data/label_all.txt | Path of the generated images for one particular .ply model. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{bakgrdir | | Path of the backgroud images sets. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{cam_head_x | 0 | Head of the camera. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{cam_head_y | 0 | Head of the camera. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{cam_head_z | -1 | Head of the camera. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{semisphere | 1 | Camera only has positions on half of the whole sphere. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{z_range | 0.6 | Maximum camera position on z axis. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{center_gen | 0 | Find center from all points. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{image_size | 128 | Size of captured images. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{label_class |  | Class label of current .ply model. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{label_item |  | Item label of current .ply model. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{rgb_use | 0 | Use RGB image or grayscale. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{num_class | 6 | Total number of classes of models. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{binary_out | 0 | Produce binaryfiles for images and label. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{view_region | 0 | Take a special view of front or back angle}&quot;</span>;</div><div class="line">    <span class="comment">/* Get parameters from comand line. */</span></div><div class="line">    <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">cv::CommandLineParser</a> parser(argc, argv, keys);</div><div class="line">    parser.about(<span class="stringliteral">&quot;Generating training data for CNN with triplet loss&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">    <span class="keywordtype">int</span> ite_depth = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;ite_depth&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> plymodel = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;plymodel&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> imagedir = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;imagedir&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> labeldir = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;labeldir&quot;</span>);</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> bakgrdir = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;bakgrdir&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> label_class = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;label_class&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> label_item = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;label_item&quot;</span>);</div><div class="line">    <span class="keywordtype">float</span> cam_head_x = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;cam_head_x&quot;</span>);</div><div class="line">    <span class="keywordtype">float</span> cam_head_y = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;cam_head_y&quot;</span>);</div><div class="line">    <span class="keywordtype">float</span> cam_head_z = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;cam_head_z&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> semisphere = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;semisphere&quot;</span>);</div><div class="line">    <span class="keywordtype">float</span> z_range = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;z_range&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> center_gen = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;center_gen&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> image_size = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;image_size&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> rgb_use = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;rgb_use&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> num_class = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;num_class&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> binary_out = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;binary_out&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> view_region = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;view_region&quot;</span>);</div><div class="line">    <span class="keywordtype">double</span> obj_dist, bg_dist, y_range;</div><div class="line">    <span class="keywordflow">if</span> (view_region == 1 || view_region == 2)</div><div class="line">    {</div><div class="line">        <span class="comment">/* Set for TV */</span></div><div class="line">        <span class="keywordflow">if</span> (label_class == 12)</div><div class="line">            obj_dist = 340;</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">            obj_dist = 250;</div><div class="line">        ite_depth = ite_depth + 1;</div><div class="line">        bg_dist = 700;</div><div class="line">        y_range = 0.85;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (view_region == 0)</div><div class="line">    {</div><div class="line">        obj_dist = 370;</div><div class="line">        bg_dist = 400;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span> (label_class == 5 || label_class == 10 || label_class == 11 || label_class == 12)</div><div class="line">        ite_depth = ite_depth + 1;</div><div class="line">    <a class="code" href="../../d2/d15/classcv_1_1cnn__3dobj_1_1icoSphere.html">cv::cnn_3dobj::icoSphere</a> ViewSphere(10,ite_depth);</div><div class="line">    std::vector&lt;cv::Point3d&gt; campos;</div><div class="line">    std::vector&lt;cv::Point3d&gt; campos_temp = ViewSphere.CameraPos;</div><div class="line">    <span class="comment">/* Regular objects on the ground using a semisphere view system */</span></div><div class="line">    <span class="keywordflow">if</span> (semisphere == 1)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> (view_region == 1)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">if</span> (campos_temp.at(pose).z &gt;= 0 &amp;&amp; campos_temp.at(pose).z &lt; z_range &amp;&amp; campos_temp.at(pose).y &lt; -y_range)</div><div class="line">                    campos.push_back(campos_temp.at(pose));</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (view_region == 2)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">if</span> (campos_temp.at(pose).z &gt;= 0 &amp;&amp; campos_temp.at(pose).z &lt; z_range &amp;&amp; campos_temp.at(pose).y &gt; y_range)</div><div class="line">                campos.push_back(campos_temp.at(pose));</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            <span class="comment">/* Set for sofa */</span></div><div class="line">            <span class="keywordflow">if</span> (label_class == 10)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">                {</div><div class="line">                    <span class="keywordflow">if</span> (campos_temp.at(pose).z &gt;= 0 &amp;&amp; campos_temp.at(pose).z &lt; z_range &amp;&amp; campos_temp.at(pose).y &lt; -0.4)</div><div class="line">                    campos.push_back(campos_temp.at(pose));</div><div class="line">                }</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">            {</div><div class="line">                <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">                {</div><div class="line">                    <span class="keywordflow">if</span> (campos_temp.at(pose).z &gt;= 0 &amp;&amp; campos_temp.at(pose).z &lt; z_range)</div><div class="line">                        campos.push_back(campos_temp.at(pose));</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="comment">/* Special object such as plane using a full space of view sphere */</span></div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> (view_region == 1)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">if</span> (campos_temp.at(pose).z &lt; 0.2 &amp;&amp; campos_temp.at(pose).z &gt; -0.2 &amp;&amp; campos_temp.at(pose).y &lt; -y_range)</div><div class="line">                    campos.push_back(campos_temp.at(pose));</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span> (view_region == 2)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">if</span> (campos_temp.at(pose).z &lt; 0.2 &amp;&amp; campos_temp.at(pose).z &gt; -0.2 &amp;&amp; campos_temp.at(pose).y &gt; y_range)</div><div class="line">                campos.push_back(campos_temp.at(pose));</div><div class="line">            }</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">        {</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos_temp.size()); pose++)</div><div class="line">            {</div><div class="line">                <span class="keywordflow">if</span> (campos_temp.at(pose).z &lt; 0.2 &amp;&amp; campos_temp.at(pose).z &gt; -0.6)</div><div class="line">                    campos.push_back(campos_temp.at(pose));</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    std::fstream imglabel;</div><div class="line">    imglabel.open(labeldir.c_str(), fstream::app|fstream::out);</div><div class="line">    <span class="keywordtype">bool</span> camera_pov = <span class="keyword">true</span>;</div><div class="line">    <span class="comment">/* Create a window using viz. */</span></div><div class="line">    <a class="code" href="../../d6/d32/classcv_1_1viz_1_1Viz3d.html">viz::Viz3d</a> myWindow(<span class="stringliteral">&quot;Coordinate Frame&quot;</span>);</div><div class="line">    <span class="comment">/* Set window size. */</span></div><div class="line">    myWindow.setWindowSize(<a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(image_size,image_size));</div><div class="line">    <span class="comment">/* Set background color. */</span></div><div class="line">    myWindow.setBackgroundColor(<a class="code" href="../../d4/dba/classcv_1_1viz_1_1Color.html#a2c1274180ee9177035d082123c5942d7">viz::Color::gray</a>());</div><div class="line">    myWindow.spinOnce();</div><div class="line">    <span class="comment">/* Create a Mesh widget, loading .ply models. */</span></div><div class="line">    <a class="code" href="../../dc/d4f/classcv_1_1viz_1_1Mesh.html">viz::Mesh</a> objmesh = <a class="code" href="../../dc/d4f/classcv_1_1viz_1_1Mesh.html#a54407fef69977bb6344f0ec2e3678083">viz::Mesh::load</a>(plymodel);</div><div class="line">    <span class="comment">/* Get the center of the generated mesh widget, cause some .ply files, this could be ignored if you are using PASCAL database*/</span></div><div class="line">    <a class="code" href="../../df/d6c/classcv_1_1Point3__.html">Point3d</a> cam_focal_point;</div><div class="line">    <span class="keywordflow">if</span> (center_gen)</div><div class="line">        cam_focal_point = ViewSphere.getCenter(objmesh.<a class="code" href="../../dc/d4f/classcv_1_1viz_1_1Mesh.html#a2719f6673412252b5539191b5d8db580">cloud</a>);</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        cam_focal_point = <a class="code" href="../../dc/d84/group__core__basic.html#ga17f8af95b76535bd387f4909850c8810">Point3d</a>(0,0,0);</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* headerPath = <span class="stringliteral">&quot;../data/header_for_&quot;</span>;</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* binaryPath = <span class="stringliteral">&quot;../data/binary_&quot;</span>;</div><div class="line">    <span class="keywordflow">if</span> (binary_out)</div><div class="line">    {</div><div class="line">        ViewSphere.createHeader(static_cast&lt;int&gt;(campos.size()), image_size, image_size, headerPath);</div><div class="line">    }</div><div class="line">    <span class="keywordtype">float</span> radius = ViewSphere.getRadius(objmesh.<a class="code" href="../../dc/d4f/classcv_1_1viz_1_1Mesh.html#a2719f6673412252b5539191b5d8db580">cloud</a>, cam_focal_point);</div><div class="line">    objmesh.<a class="code" href="../../dc/d4f/classcv_1_1viz_1_1Mesh.html#a2719f6673412252b5539191b5d8db580">cloud</a> = objmesh.<a class="code" href="../../dc/d4f/classcv_1_1viz_1_1Mesh.html#a2719f6673412252b5539191b5d8db580">cloud</a>/radius*100;</div><div class="line">    cam_focal_point = cam_focal_point/radius*100;</div><div class="line">    <a class="code" href="../../df/d6c/classcv_1_1Point3__.html">Point3d</a> cam_y_dir;</div><div class="line">    cam_y_dir.<a class="code" href="../../df/d6c/classcv_1_1Point3__.html#a363a0ec9e4ebce7209438cb14c29ab47">x</a> = cam_head_x;</div><div class="line">    cam_y_dir.<a class="code" href="../../df/d6c/classcv_1_1Point3__.html#aa332b734e658368c647744adb9065810">y</a> = cam_head_y;</div><div class="line">    cam_y_dir.<a class="code" href="../../df/d6c/classcv_1_1Point3__.html#a1af9c182772a3c082e505f50eb5d2ee1">z</a> = cam_head_z;</div><div class="line">    <span class="keywordtype">char</span> temp[1024];</div><div class="line">    std::vector&lt;String&gt; name_bkg;</div><div class="line">    <span class="keywordflow">if</span> (bakgrdir.size() != 0)</div><div class="line">    {</div><div class="line">        <span class="comment">/* List the file names under a given path */</span></div><div class="line">        listDir(bakgrdir.c_str(), name_bkg, <span class="keyword">false</span>);</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i = 0; i &lt; name_bkg.size(); i++)</div><div class="line">        {</div><div class="line">            name_bkg.at(i) = bakgrdir + name_bkg.at(i);</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="comment">/* Images will be saved as .png files. */</span></div><div class="line">    <span class="keywordtype">size_t</span> cnt_img;</div><div class="line">    srand((<span class="keywordtype">int</span>)time(0));</div><div class="line">    <span class="keywordflow">do</span></div><div class="line">    {</div><div class="line">        cnt_img = 0;</div><div class="line">        <span class="keywordflow">for</span>(<span class="keywordtype">int</span> pose = 0; pose &lt; static_cast&lt;int&gt;(campos.size()); pose++){</div><div class="line">            <span class="comment">/* Add light. */</span></div><div class="line">            <span class="comment">// double alpha1 = rand()%(314/2)/100;</span></div><div class="line">            <span class="comment">// double alpha2 = rand()%(314*2)/100;</span></div><div class="line">            <span class="comment">// printf(&quot;%f %f %f/n&quot;, ceil(10000*sqrt(1 - sin(alpha1)*sin(alpha1))*sin(alpha2)), 10000*sqrt(1 - sin(alpha1)*sin(alpha1))*cos(alpha2), sin(alpha1)*10000);</span></div><div class="line">            <span class="comment">// myWindow.addLight(Vec3d(10000*sqrt(1 - sin(alpha1)*sin(alpha1))*sin(alpha2),10000*sqrt(1 - sin(alpha1)*sin(alpha1))*cos(alpha2),sin(alpha1)*10000), Vec3d(0,0,0), viz::Color::white(), viz::Color::white(), viz::Color::black(), viz::Color::white());</span></div><div class="line">            <span class="keywordtype">int</span> label_x, label_y, label_z;</div><div class="line">            label_x = <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>(campos.at(pose).x*100);</div><div class="line">            label_y = <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>(campos.at(pose).y*100);</div><div class="line">            label_z = <span class="keyword">static_cast&lt;</span><span class="keywordtype">int</span><span class="keyword">&gt;</span>(campos.at(pose).z*100);</div><div class="line">            sprintf (temp,<span class="stringliteral">&quot;%02i_%02i_%04i_%04i_%04i_%02i&quot;</span>, label_class, label_item, label_x, label_y, label_z, static_cast&lt;int&gt;(obj_dist/100));</div><div class="line">            <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = temp;</div><div class="line">            filename += <span class="stringliteral">&quot;.png&quot;</span>;</div><div class="line">            imglabel &lt;&lt; filename &lt;&lt; <span class="charliteral">&#39; &#39;</span> &lt;&lt; label_class &lt;&lt; endl;</div><div class="line">            filename = imagedir + filename;</div><div class="line">            <span class="comment">/* Get the pose of the camera using makeCameraPoses. */</span></div><div class="line">            <span class="keywordflow">if</span> (view_region != 0)</div><div class="line">            {</div><div class="line">                cam_focal_point.<a class="code" href="../../df/d6c/classcv_1_1Point3__.html#a363a0ec9e4ebce7209438cb14c29ab47">x</a> = cam_focal_point.<a class="code" href="../../df/d6c/classcv_1_1Point3__.html#aa332b734e658368c647744adb9065810">y</a> - label_x/5;</div><div class="line">            }</div><div class="line">            <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> cam_pose = <a class="code" href="../../d1/d19/group__viz.html#gab04d43faa448940bb58754af6356955b">viz::makeCameraPose</a>(campos.at(pose)*obj_dist+cam_focal_point, cam_focal_point, cam_y_dir*obj_dist+cam_focal_point);</div><div class="line">            <span class="comment">/* Get the transformation matrix from camera coordinate system to global. */</span></div><div class="line">            <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> <a class="code" href="../../d2/de8/group__core__array.html#ga393164aa54bb9169ce0a8cc44e08ff22">transform</a> = <a class="code" href="../../d1/d19/group__viz.html#ga16ef52f42849f13a36b715d488ce5b9f">viz::makeTransformToGlobal</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(1.0f,0.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,1.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,0.0f,1.0f), campos.at(pose));</div><div class="line">            <a class="code" href="../../d2/d76/classcv_1_1viz_1_1WMesh.html">viz::WMesh</a> mesh_widget(objmesh);</div><div class="line">            <span class="comment">/* Pose of the widget in camera frame. */</span></div><div class="line">            <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> cloud_pose = <a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a>().<a class="code" href="../../dd/d99/classcv_1_1Affine3.html#a11d9cdc588a9c08c0c1c205a872663fd">translate</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(1.0f,1.0f,1.0f));</div><div class="line">            <span class="comment">/* Pose of the widget in global frame. */</span></div><div class="line">            <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> cloud_pose_global = transform * cloud_pose;</div><div class="line">            <span class="comment">/* Visualize camera frame. */</span></div><div class="line">            <span class="keywordflow">if</span> (!camera_pov)</div><div class="line">            {</div><div class="line">                <a class="code" href="../../d6/dcc/classcv_1_1viz_1_1WCameraPosition.html">viz::WCameraPosition</a> cpw(1); <span class="comment">// Coordinate axes</span></div><div class="line">                <a class="code" href="../../d6/dcc/classcv_1_1viz_1_1WCameraPosition.html">viz::WCameraPosition</a> cpw_frustum(<a class="code" href="../../dc/d84/group__core__basic.html#ga392bb4f8a6b9e0dde07f31dc28e73319">Vec2f</a>(0.5, 0.5)); <span class="comment">// Camera frustum</span></div><div class="line">                myWindow.showWidget(<span class="stringliteral">&quot;CPW&quot;</span>, cpw, cam_pose);</div><div class="line">                myWindow.showWidget(<span class="stringliteral">&quot;CPW_FRUSTUM&quot;</span>, cpw_frustum, cam_pose);</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="comment">/* Visualize widget. */</span></div><div class="line">            <span class="keywordflow">if</span> (bakgrdir.size() != 0)</div><div class="line">            {</div><div class="line">                <a class="code" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a> img_bg = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv::imread</a>(name_bkg.at(rand()%name_bkg.size()));</div><div class="line">                <span class="comment">/* Back ground images has a distance of 2 times of radius of camera view distance */</span></div><div class="line">                <a class="code" href="../../d7/d3d/classcv_1_1viz_1_1WImage3D.html">cv::viz::WImage3D</a> background_widget(img_bg, <a class="code" href="../../dc/d84/group__core__basic.html#gaff96e5bec8019ca9917fd1e4c0cf8267">Size2d</a>(image_size*4.2, image_size*4.2), <a class="code" href="../../dc/d84/group__core__basic.html#ga370d94209693b5b13437ab4991cabf73">Vec3d</a>(-campos.at(pose)*bg_dist+cam_focal_point), <a class="code" href="../../dc/d84/group__core__basic.html#ga370d94209693b5b13437ab4991cabf73">Vec3d</a>(campos.at(pose)*bg_dist-cam_focal_point), <a class="code" href="../../dc/d84/group__core__basic.html#ga370d94209693b5b13437ab4991cabf73">Vec3d</a>(0,0,-1)*bg_dist+<a class="code" href="../../dc/d84/group__core__basic.html#ga370d94209693b5b13437ab4991cabf73">Vec3d</a>(0,2*cam_focal_point.y,0));</div><div class="line">                myWindow.showWidget(<span class="stringliteral">&quot;bgwidget&quot;</span>, background_widget, cloud_pose_global);</div><div class="line">            }</div><div class="line">            <span class="comment">// mesh_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);</span></div><div class="line">            myWindow.showWidget(<span class="stringliteral">&quot;targetwidget&quot;</span>, mesh_widget, cloud_pose_global);</div><div class="line"></div><div class="line">            <span class="comment">/* Set the viewer pose to that of camera. */</span></div><div class="line">            <span class="keywordflow">if</span> (camera_pov)</div><div class="line">                myWindow.setViewerPose(cam_pose);</div><div class="line">            <span class="comment">/* Save screen shot as images. */</span></div><div class="line">            myWindow.saveScreenshot(filename);</div><div class="line">            <span class="keywordflow">if</span> (binary_out)</div><div class="line">            {</div><div class="line">            <span class="comment">/* Write images into binary files for further using in CNN training. */</span></div><div class="line">                ViewSphere.writeBinaryfile(filename, binaryPath, headerPath,static_cast&lt;int&gt;(campos.size())*num_class, label_class, static_cast&lt;int&gt;(campos.at(pose).x*100), static_cast&lt;int&gt;(campos.at(pose).y*100), static_cast&lt;int&gt;(campos.at(pose).z*100), rgb_use);</div><div class="line">            }</div><div class="line">            cnt_img++;</div><div class="line">        }</div><div class="line">    } <span class="keywordflow">while</span> (cnt_img != campos.size());</div><div class="line">    imglabel.close();</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">};</div></div><!-- fragment --><h2>Explanation </h2>
<p>Here is the general structure of the program:</p>
<ul>
<li>Create a window. <div class="fragment"><div class="line">viz::Viz3d myWindow(<span class="stringliteral">&quot;Coordinate Frame&quot;</span>);</div></div><!-- fragment --></li>
<li>Set window size as 64*64, we use this scale as default. <div class="fragment"><div class="line">myWindow.setWindowSize(<a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(64,64));</div></div><!-- fragment --></li>
<li>Add coordinate axes. <div class="fragment"><div class="line">myWindow.showWidget(<span class="stringliteral">&quot;Coordinate Widget&quot;</span>, viz::WCoordinateSystem());</div><div class="line">myWindow.setBackgroundColor(viz::Color::gray());</div><div class="line">myWindow.spin();</div></div><!-- fragment --></li>
<li>Create a Mesh widget, loading .ply models. <div class="fragment"><div class="line">viz::Mesh objmesh = viz::Mesh::load(plymodel);</div></div><!-- fragment --></li>
<li>Get the center of the generated mesh widget, cause some .ply files. <div class="fragment"><div class="line"><a class="code" href="../../dc/d84/group__core__basic.html#ga17f8af95b76535bd387f4909850c8810">Point3d</a> cam_focal_point = ViewSphere.getCenter(objmesh.cloud);</div></div><!-- fragment --></li>
<li>Get the pose of the camera using makeCameraPoses. <div class="fragment"><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> cam_pose = <a class="code" href="../../d1/d19/group__viz.html#gab04d43faa448940bb58754af6356955b">viz::makeCameraPose</a>(campos.at(pose)*radius+cam_focal_point, cam_focal_point, cam_y_dir*radius+cam_focal_point);</div></div><!-- fragment --></li>
<li>Get the transformation matrix from camera coordinate system to global. <div class="fragment"><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> transform = <a class="code" href="../../d1/d19/group__viz.html#ga16ef52f42849f13a36b715d488ce5b9f">viz::makeTransformToGlobal</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(1.0f,0.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,1.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,0.0f,1.0f), campos.at(pose));</div><div class="line">viz::WMesh mesh_widget(objmesh);</div></div><!-- fragment --></li>
<li>Save screen shot as images. <div class="fragment"><div class="line">myWindow.saveScreenshot(filename);</div></div><!-- fragment --></li>
<li>Write images into binary files for further using in CNN training. <div class="fragment"><div class="line">ViewSphere.writeBinaryfile(filename, binaryPath, headerPath,(<span class="keywordtype">int</span>)campos.size()*num_class, label_class, (int)(campos.at(pose).x*100), (<span class="keywordtype">int</span>)(campos.at(pose).y*100), (<span class="keywordtype">int</span>)(campos.at(pose).z*100), rgb_use);</div></div><!-- fragment --></li>
</ul>
<h2>Results </h2>
<p>Here is collection images created by this demo using 4 model.</p>
<div class="image">
<img src="../../images_all/1_8.png"/>
</div>
 </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:38 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
